View Javadoc

1   package com.nexes.wizard;
2   
3   import java.awt.event.ActionListener;
4   
5   /***
6    * This class is responsible for reacting to events generated by pushing any of the
7    * three buttons, 'Next', 'Previous', and 'Cancel.' Based on what button is pressed,
8    * the controller will update the model to show a new panel and reset the state of
9    * the buttons as necessary.
10   */
11  public class WizardController implements ActionListener {
12      
13      private Wizard wizard;
14      
15      /***
16       * This constructor accepts a reference to the Wizard component that created it,
17       * which it uses to update the button components and access the WizardModel.
18       * @param w A callback to the Wizard component that created this controller.
19       */    
20      public WizardController(Wizard w) {
21          wizard = w;
22      }
23  
24      /***
25       * Calling method for the action listener interface. This class listens for actions
26       * performed by the buttons in the Wizard class, and calls methods below to determine
27       * the correct course of action.
28       * @param evt The ActionEvent that occurred.
29       */    
30      public void actionPerformed(java.awt.event.ActionEvent evt) {
31          
32          if (evt.getActionCommand().equals(Wizard.CANCEL_BUTTON_ACTION_COMMAND))
33              cancelButtonPressed();
34          else if (evt.getActionCommand().equals(Wizard.BACK_BUTTON_ACTION_COMMAND))
35              backButtonPressed();
36          else if (evt.getActionCommand().equals(Wizard.NEXT_BUTTON_ACTION_COMMAND))
37              nextButtonPressed();
38          
39      }
40      
41      
42      
43      private void cancelButtonPressed() {
44          
45          wizard.close(Wizard.CANCEL_RETURN_CODE);
46      }
47  
48      private void nextButtonPressed() {
49   
50          WizardModel model = wizard.getModel();
51          WizardPanelDescriptor descriptor = model.getCurrentPanelDescriptor();
52          
53          //  If it is a finishable panel, close down the dialog. Otherwise,
54          //  get the ID that the current panel identifies as the next panel,
55          //  and display it.
56          
57          Object nextPanelDescriptor = descriptor.getNextPanelDescriptor();
58          
59          if (nextPanelDescriptor instanceof WizardPanelDescriptor.FinishIdentifier) {
60              wizard.close(Wizard.FINISH_RETURN_CODE);
61          } else {        
62              wizard.setCurrentPanel(nextPanelDescriptor);
63          }
64          
65      }
66  
67      private void backButtonPressed() {
68   
69          WizardModel model = wizard.getModel();
70          WizardPanelDescriptor descriptor = model.getCurrentPanelDescriptor();
71   
72          //  Get the descriptor that the current panel identifies as the previous
73          //  panel, and display it.
74          
75          Object backPanelDescriptor = descriptor.getBackPanelDescriptor();        
76          wizard.setCurrentPanel(backPanelDescriptor);
77          
78      }
79  
80      
81      void resetButtonsToPanelRules() {
82      
83          //  Reset the buttons to support the original panel rules,
84          //  including whether the next or back buttons are enabled or
85          //  disabled, or if the panel is finishable.
86          
87          WizardModel model = wizard.getModel();
88          WizardPanelDescriptor descriptor = model.getCurrentPanelDescriptor();
89          
90          model.setCancelButtonText(Wizard.CANCEL_TEXT);
91          model.setCancelButtonIcon(Wizard.CANCEL_ICON);
92          
93          //  If the panel in question has another panel behind it, enable
94          //  the back button. Otherwise, disable it.
95          
96          model.setBackButtonText(Wizard.BACK_TEXT);
97          model.setBackButtonIcon(Wizard.BACK_ICON);
98          
99          if (descriptor.getBackPanelDescriptor() != null)
100             model.setBackButtonEnabled(Boolean.TRUE);
101         else
102             model.setBackButtonEnabled(Boolean.FALSE);
103 
104         //  If the panel in question has one or more panels in front of it,
105         //  enable the next button. Otherwise, disable it.
106  
107         if (descriptor.getNextPanelDescriptor() != null)
108             model.setNextFinishButtonEnabled(Boolean.TRUE);
109         else
110             model.setNextFinishButtonEnabled(Boolean.FALSE);
111  
112         //  If the panel in question is the last panel in the series, change
113         //  the Next button to Finish. Otherwise, set the text back to Next.
114         
115         if (descriptor.getNextPanelDescriptor() instanceof WizardPanelDescriptor.FinishIdentifier) {
116             model.setNextFinishButtonText(Wizard.FINISH_TEXT);
117             model.setNextFinishButtonIcon(Wizard.FINISH_ICON);
118         } else {
119             model.setNextFinishButtonText(Wizard.NEXT_TEXT);
120             model.setNextFinishButtonIcon(Wizard.NEXT_ICON);
121         }
122         
123     }
124     
125     
126 }